<?php

namespace apiend\models\searchs;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * ProductSearch is the search model class for table "{{%porduct}}".
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
abstract class ProductSearch extends \common\logics\ShopProductLogic implements \frontend\models\SearchModelInterface {

    /**
     * 条件检索
     * @param mixed $cacheDuration 缓存时间
     * @return ActiveDataProvider
     */
    public function search($cacheDuration = 60) {
        $query = $this->buildQuery();
        Yii::error($query->debug());
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
//        if (!YII_DEBUG && $cacheDuration !== false) {
//            Yii::$app->db->cache(function($db) use($dataProvider) {
//                $dataProvider->prepare();
//            }, $cacheDuration);
//        }
        return $dataProvider;
    }

    /**
     * 按数量条件检索
     * @param mixed $limit 查询数量
     * @return ActiveDataProvider
     */
    public function limitsSearch($limit = 6, $cacheDuration = 60) {
        $query = $this->buildQuery();
        if (!YII_DEBUG && $cacheDuration !== false) {
            $query->cache($cacheDuration);
        }
        if ($limit !== false) {
            $query->limit($limit);
        }
        return $query->all();
    }

}
